package com.whfc.fvs.service.impl;

import com.whfc.common.enums.DelFlag;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.ResultEnum;
import com.whfc.fvs.dao.FvsDeviceMapper;
import com.whfc.fvs.dao.FvsSettingMapper;
import com.whfc.fvs.dao.FvsStreamMapper;
import com.whfc.fvs.dto.*;
import com.whfc.fvs.entity.FvsDevice;
import com.whfc.fvs.entity.FvsStream;
import com.whfc.fvs.enums.FvsDevicePtzOption;
import com.whfc.fvs.enums.FvsDeviceStatus;
import com.whfc.fvs.enums.VsPlatform;
import com.whfc.fvs.param.FvsDeviceAddParam;
import com.whfc.fvs.service.FvsPlatformService;
import com.whfc.fvs.service.factroy.FvsPlatformConstant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.MessageFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 自定义流媒体服务器
 *
 * @ author: qzexing
 * @ version: 1.0
 * @ date: 2020/6/5 11:18
 */
@Service(FvsPlatformConstant.STREAM_SERVER_SERVICE)
public class FvsStreamServerServiceImpl implements FvsPlatformService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FvsStreamMapper fvsStreamMapper;

    @Autowired
    private FvsDeviceMapper fvsDeviceMapper;

    @Autowired
    private FvsSettingMapper fvsSettingMapper;

    @Value("${whfc.default.demo.project}")
    private Integer defaultDeptId;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDevice(FvsDeviceDTO fvsDeviceDTO, FvsDeviceAddParam deviceAddParam) {
        String streamName = deviceAddParam.getUsername();
        if (StringUtils.isEmpty(streamName)) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "流ID不能为空");
        }
        FvsDevice fvsDevice = new FvsDevice();
        fvsDevice.setId(fvsDeviceDTO.getId());
        fvsDevice.setUsername(streamName);
        fvsDevice.setDeviceStatus(FvsDeviceStatus.ONLINE.getValue());
        FvsSettingDTO fvsSettingDTO = fvsSettingMapper.selectSrsInfoByDeptId(fvsDeviceDTO.getDeptId(), VsPlatform.STREAM_SERVER.getValue());
        if (fvsSettingDTO == null) {
            //获取默认的项目
            fvsSettingDTO = fvsSettingMapper.selectSrsInfoByDeptId(defaultDeptId, VsPlatform.STREAM_SERVER.getValue());
        }
        String srsSnapshot = fvsSettingDTO.getSrsSnapshot();
        String snapShotUrl = MessageFormat.format(srsSnapshot, streamName);
        fvsDevice.setSnapshotUrl(snapShotUrl);
        fvsDeviceMapper.updateByPrimaryKeySelective(fvsDevice);
        FvsStream fvsStream = new FvsStream();
        String srsRtmp = fvsSettingDTO.getSrsRtmp();
        String rtmpUrl = MessageFormat.format(srsRtmp, streamName);
        fvsStream.setRtmp(rtmpUrl);
        String srsHls = fvsSettingDTO.getSrsHls();
        String hlsUrl = MessageFormat.format(srsHls, streamName);
        fvsStream.setHls(hlsUrl);
        fvsStream.setDeviceId(fvsDevice.getId());
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2099);
        fvsStream.setExpireTime(calendar.getTime());
        fvsStream.setDelFlag(DelFlag.UNDELETE.getValue());
        fvsStreamMapper.insertSelective(fvsStream);
    }


    @Override
    public FvsStreamDTO getStreamUrl(FvsDeviceDTO fvsDeviceDTO) {
        return fvsStreamMapper.selectByDeviceId(fvsDeviceDTO.getId());
    }

    @Override
    public void deleteDevice(FvsDeviceDTO fvsDeviceDTO) {
    }

    @Override
    public String getSnapshotUrl(FvsDeviceDTO fvsDeviceDTO) {
        return fvsDeviceDTO.getSnapshotUrl();
    }

    @Override
    public ListData<FvsRecordDTO> getRecordList(FvsDeviceDTO fvsDeviceDTO, Date startTime, Date endTime) {
        return null;
    }

    @Override
    public void ptz(FvsDeviceDTO fvsDeviceDTO, FvsDevicePtzOption devicePtzOption) {
    }

    @Override
    public FvsTokenDTO getToken(Integer deptId) {
        return null;
    }

}
